package com.novax.ex.activity.provider.controller;

import cn.hutool.core.bean.BeanUtil;
import com.novax.ex.activity.infrastructure.entity.MallOrderWinnerImgs;
import com.novax.ex.activity.open.api.inner.MallOrderWinnerImgsInnerApi;
import com.novax.ex.activity.open.model.query.MallWinnerImgQuery;
import com.novax.ex.activity.open.model.request.MallWinnerImgRequest;
import com.novax.ex.activity.open.model.response.MallWinnerImgResponse;
import com.novax.ex.activity.open.model.response.MallWinnerRecordResponse;
import com.novax.ex.activity.provider.service.MallOrderWinnerImgsService;
import com.novax.ex.activity.provider.service.MallOrderWinnerService;
import com.novax.ex.common.constant.InternationalCode;
import com.novax.ex.common.constant.StatusCode;
import com.novax.ex.common.results.ReturnResult;
import com.novax.ex.common.util.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * Description: 商品获得情况-图片管理
 *
 * @author Deucalion@novax.email
 * @date 2023/2/23 17:29
 */
@Slf4j
@RestController
public class MallOrderWinnerImgsController extends BaseActivityController implements MallOrderWinnerImgsInnerApi {
    @Resource
    private MallOrderWinnerImgsService mallOrderWinnerImgsService;

    @Override
    public ReturnResult add(String language, Long adminId, MallWinnerImgRequest request, BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            return ReturnResult.fail(getInternational(language, InternationalCode.LACK_VALUE));
        }
        MallOrderWinnerImgs entity = BeanUtil.copyProperties(request, MallOrderWinnerImgs.class);
        entity.setId(null);
        entity.setCreator(adminId);
        boolean result = mallOrderWinnerImgsService.addSelective(entity);
        if (result) {
            return ReturnResult.success(getInternational(language, InternationalCode.OPERATION_SUCCESS));
        }
        return ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
    }

    @Override
    public ReturnResult modify(String language, Long adminId, MallWinnerImgRequest request, BindingResult bindingResult) {
        if(null == request.getId()){
            return ReturnResult.fail(getInternational(language, InternationalCode.LACK_VALUE));
        }
        if(bindingResult.hasErrors()){
            return ReturnResult.fail(getInternational(language, InternationalCode.LACK_VALUE));
        }
        MallOrderWinnerImgs old = mallOrderWinnerImgsService.find(request.getId());
        if(null == old){
            return ReturnResult.fail(getInternational(language, InternationalCode.NOT_EXIST));
        }
        MallOrderWinnerImgs entity = BeanUtil.copyProperties(request, MallOrderWinnerImgs.class);
        entity.setUpdater(adminId);
        boolean result = mallOrderWinnerImgsService.modifySelective(entity);
        if (result) {
            return ReturnResult.success(getInternational(language, InternationalCode.OPERATION_SUCCESS));
        }
        return ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
    }

    @Override
    public ReturnResult remove(Long id, String language, Long adminId) {
        MallOrderWinnerImgs old = mallOrderWinnerImgsService.find(id);
        if(null == old){
            return ReturnResult.fail(getInternational(language, InternationalCode.NOT_EXIST));
        }
        boolean result = mallOrderWinnerImgsService.remove(id);
        if (result) {
            return ReturnResult.success(getInternational(language, InternationalCode.OPERATION_SUCCESS));
        }
        return ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
    }

    @Override
    public ReturnResult<PageUtil<MallWinnerImgResponse>> list(String language, MallWinnerImgQuery query) {
        PageUtil<MallWinnerImgResponse> pages = new PageUtil<>(query);
        Map<String, Object> condition = new HashMap<>();
        condition.put("query", query);
        pages.setCondition(condition);
        mallOrderWinnerImgsService.findPage(pages,MallWinnerImgResponse.class);
        return ReturnResult.success(getInternational(language, InternationalCode.QUERY_SUCCESS), pages);
    }

    @Override
    public ReturnResult<MallWinnerImgResponse> detail(Long id, String language) {
        MallOrderWinnerImgs old = mallOrderWinnerImgsService.find(id);
        if(null == old){
            return ReturnResult.fail(getInternational(language, InternationalCode.NOT_EXIST));
        }
        MallWinnerImgResponse response = BeanUtil.copyProperties(old, MallWinnerImgResponse.class);
        return ReturnResult.success(getInternational(language, InternationalCode.QUERY_SUCCESS), response);
    }
}
